package model;

import java.util.LinkedList;
import java.util.Observable;
import java.util.Random;
//import java.util.Scanner;
import view.GamePanel;
import view.InfoPanel;

public class Snake extends Observable { // 设置Snake为被观察者

	public static final int UP = 0x100;
	public static final int RIGHT = 0x101;
	public static final int DOWN = 0x102;
	public static final int LEFT = 0x103;
	public static final int RUNNING = 0x500;
	public static final int PASE = 0x501;
	public static final int OVER = 0x502;
	public static final int NOSTART = 0x503;

	
	private int score;
	public int speed;
	public boolean run_state;
	private LinkedList<Node> body; // 双链表对象body<节点>
	private int direction;        //方向
	private int state;
	private Node food;
    
	

	public Snake() {

		body = new LinkedList<>(); // 创建对象
        state =NOSTART;
          }

	public void makeFood() {
		Random rd = new Random();
		int x = rd.nextInt(GamePanel.WIDTH / Node.WIDTH);
		int y = rd.nextInt(GamePanel.HEIGHT / Node.HEIGHT);
		food = new Node(x * Node.WIDTH, y * Node.HEIGHT);
		setChanged();
		notifyObservers(food);
	}

	public void init() {
		run_state=true;
		direction = RIGHT; // 默认往右
		InfoPanel.label[2].setText("慢速");
		InfoPanel.label[1].setText("0");
		score=0;
		speed=500;
		state = RUNNING;
		body.clear();
		body.addLast(new Node(80, 20));
		body.addLast(new Node(60, 20));
		body.addLast(new Node(40, 20));
		setChanged(); // 获取变化
		notifyObservers(body); // 把body的变化通知给观察者
		makeFood();
	}

	
	   
	
	
	public void changeDirection(int direction) { // 新建改变方向的方法
		// TODO Auto-generated method stub
		if (direction % 2 != this.direction % 2) { // 如果不是要往前进方向的反方向改变（如果前进方向与2的求余不等于点击方向与2的求余）
			this.direction = direction; // 就把方向的值赋给当前方向

		}
	}

	private void move(InfoListener info) {
		
		if (body.size() < 3)
			return;
		Node tail = body.removeLast();
		Node head = body.getFirst();
		int x = head.getX(), y = head.getY();
		//run_state=true;
		
	
		switch (direction) {
		case RIGHT:
			x = x + Node.WIDTH;
			break;
		case LEFT:
			x = x - Node.WIDTH;
			break;
		case UP:
			y = y - Node.HEIGHT;
			break;
		case DOWN:
			y = y + Node.HEIGHT;
			break;

		}
		
		tail.setX(x);
		tail.setY(y);
		body.addFirst(tail);
		if (gameOver())
			{info.infoChange();return;}
		setChanged();
		notifyObservers(body);
		eat();

	}
	
	public boolean gameOver() {
		Node head = body.getFirst();
		int x = head.getX();
		int y = head.getY();
		if (x > 780) {
			state = OVER;
			return true;
		}
		if (y > 780) {
			state = OVER;
			return true;
		}
		if (x < 0) {
			state = OVER;
			return true;
		}
		if (y < 0) {
			state = OVER;
			return true;
		}
		for (int i = 4;i<body.size(); i++) {              //size()获取集合类的大小
			if (head.getX() == body.get(i).getX() && head.getY() == body.get(i).getY()) {
				state = OVER;
				return true;
			}
		}
		return false;
	}

	private void eat() {
		// TODO Auto-generated method stub
		Node head = body.getFirst();
		if (head.getX() == food.getX() && head.getY() == food.getY()) {
			
				score+=10;
		InfoPanel.label[1].setText(String.valueOf(score));
			
			body.addLast(food);
			makeFood();
            
		}
		setChanged();
		notifyObservers(body);

	}

	public void openMoveThread(InfoListener info) {
		// Scanner input=new Scanner(System.in);
		// System.out.println("请输入贪吃蛇的速度0-999");

		new Thread(new Runnable() {
			public void run() {
				while (true) {
					//System.out.println(state);
					if (state == RUNNING&&run_state==true)
						{move(info);}
					else if(state == RUNNING&&run_state==false)
					   {InfoPanel.label[0].setText("暂停");}
					
					try {
						// Thread.sleep(1000-input.nextInt());
						Thread.sleep(speed);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		}).start();

	}
	public int getState() {
		return state;
	}
}
